package Controller;

import java.util.ArrayList;
import Model.Loan;
import Model.Person;
import Model.Copy;

/*
 * Application logic for loaning a copy of DVD to a person.
 */

public class LoanCtr
{
    // fields
    // list of all loans
    private ArrayList<Loan> loans;
    // unique ID for each loan
    private int loanUniqueID = 0;
    // address and dvd controllers
    private AddressCtr addressCtr = AddressCtr.getInstance();
    private DVDCtr dvdCtr = DVDCtr.getInstance();
    private static LoanCtr instance = null;
    
    private LoanCtr()
    {
        loans = new ArrayList<Loan>();
    }
    
    public static LoanCtr getInstance()
    {
        if(instance == null) {
            instance = new LoanCtr();
        }
        return instance;
    }
    
    // creating a loan person specific ID and copy serial number are needed
    public void createLoan(String borrowDate, String period, int personID, String copySerialNumber)
    {
        // checking if the copy you want to loan is already loaned.
        boolean available = true;
        for (Loan loan : loans) { 
            if(loan.getCopy().getSerialNumber().equals(copySerialNumber) && loan.getState().equals("loaned")) {
                available = false;
            }
        }
        
        if (available) {
            // to set up state of the loan
            String state = "loaned";
            // looking for specified copy to loan
            Copy copy = dvdCtr.findCopy(copySerialNumber);
            // looking for specific person 
            Person person = addressCtr.findPerson(personID);
            // creating a loan with all needed paramenters
            Loan loan = new Loan(loanUniqueID, borrowDate, period, state, person, copy);
            // adding it to list of loans
            loans.add(loan);
            // increasing the ID for next loan
            loanUniqueID++;
        } else {
            System.out.println("copy is already loaned");
        }
    }
    
    // returns String representation of loan information specified by person ID
    public String findByPerson(int personId)
    {
        String loanInfo = new String();
        for(Loan loan : loans){
            int id = loan.getPerson().getId();
            if(personId == id){
                loanInfo = loanInfo + loan.toString();
            }
        }
        return loanInfo;
    }
    
    // returns String representation of loan information specified by copy serial number
    public String findByCopy(String serialNumber)
    {
        String loanInfo = new String();
        for(Loan loan : loans)
        {
            String copySerialNumber = loan.getCopy().getSerialNumber();
            if(copySerialNumber.equals(serialNumber)) {
                loanInfo = loanInfo + loan.toString();
            }
        }
        return loanInfo;
    }
    
    // change loan period in specified loan object
    public void updateLoanPeriod(int loanId, String newPeriod)
    {
        for (Loan loan : loans)
        {
            if (loan.getId() == loanId) {
                loan.setPeriod(newPeriod);
            }
        }
    }
    
    // change status of loan to "returned", and ending a loan
    public void endLoan(int loanId)
    {
        for (Loan loan : loans)
        {
            if(loan.getId() == loanId) {
                loan.setState("returned");
            }
        }
    }
}
